package com.bawei.module_message.ui

import com.bawei.lib_common.base.BaseViewModel
//import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.module_message.model.MessageEvent
import com.bawei.module_message.model.MessageModelItem
import com.bawei.module_message.model.RequestEvent
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class MessageViewModel @Inject constructor(private val repository: MessageRepository) : BaseViewModel() {


    // 用 MutableStateFlow 管理三类消息的独立状态
    private val _systemNoticeState = MutableStateFlow<MessageEvent>(
        MessageEvent.SystemNotice(RequestEvent.Loading)
    )
    val systemNoticeState: StateFlow<MessageEvent> = _systemNoticeState.asStateFlow()

    private val _propertyServiceState = MutableStateFlow<MessageEvent>(
        MessageEvent.PropertyService(RequestEvent.Loading)
    )
    val propertyServiceState: StateFlow<MessageEvent> = _propertyServiceState.asStateFlow()

    private val _parkingManageState = MutableStateFlow<MessageEvent>(
        MessageEvent.ParkingManage(RequestEvent.Loading)
    )
    val parkingManageState: StateFlow<MessageEvent> = _parkingManageState.asStateFlow()


//    private val mMessage_ = MutableStateFlow<RequestEvent>(Loading)
//    val mMessageState = mMessage_.asStateFlow()

    private var mMessageListData = listOf<MessageModelItem>()

    // 系统通知列表请求
    fun fetchSystemNotice() {
        execute(
            action = repository.getMessage(accessId = "111122223333", userId = 1),
            onStart = {
                _systemNoticeState.value = MessageEvent.SystemNotice(RequestEvent.Loading)
            },
            onFinish = { result ->
                _systemNoticeState.value = MessageEvent.SystemNotice(
                    RequestEvent.Success(result.data)
                )
            },
            onError = {
                _systemNoticeState.value = MessageEvent.SystemNotice(
                    RequestEvent.Failed(it ?: "请求失败")
                )
            }
        )
    }

    // 物业服务列表请求
    fun fetchPropertyService() {
        execute(
            action = repository.getMessage(accessId = "111122223333", userId = 2),
            onStart = {
                _propertyServiceState.value = MessageEvent.PropertyService(RequestEvent.Loading)
            },
            onFinish = { result ->
                _propertyServiceState.value = MessageEvent.PropertyService(
                    RequestEvent.Success(result.data)
                )
            },
            onError = {
                _propertyServiceState.value = MessageEvent.PropertyService(
                    RequestEvent.Failed(it ?: "请求失败")
                )
            }
        )
    }

    // 车位管理列表请求
    fun fetchParkingManage() {
        execute(
            action = repository.getMessage(accessId = "111122223333", userId = 3),
            onStart = {
                _parkingManageState.value = MessageEvent.ParkingManage(RequestEvent.Loading)
            },
            onFinish = { result ->
                _parkingManageState.value = MessageEvent.ParkingManage(
                    RequestEvent.Success(result.data)
                )
            },
            onError = {
                _parkingManageState.value = MessageEvent.ParkingManage(
                    RequestEvent.Failed(it ?: "请求失败")
                )
            }
        )
    }

//    fun messageList() {
//        execute(
//            action = repository.getMessage("111122223333", 1),
//            onStart = {
//                mMessage_.value = Loading
//            },
//            onFinish = { result ->
//                mMessageListData = result.data
//                mMessage_.value = Success
//            },
//            onError = {
//                mMessage_.value =  Failed(it)
//            },
//        )
//    }
//    fun messageList1() {
//        execute(
//            action = repository.getMessage("111122223333", 2),
//            onStart = {
//                mMessage_.value = Loading
//            },
//            onFinish = { result ->
//                mMessageListData = result.data
//                mMessage_.value = Success
//            },
//            onError = {
//                mMessage_.value =  Failed(it)
//            },
//        )
//    }
//    fun messageList2() {
//        execute(
//            action = repository.getMessage("111122223333", 3),
//            onStart = {
//                mMessage_.value = Loading
//            },
//            onFinish = { result ->
//                mMessageListData = result.data
//                mMessage_.value = Success
//            },
//            onError = {
//                mMessage_.value =  Failed(it)
//            },
//        )
//    }



}






























